// ==UserScript==
// @name         坏医生自动播放与答题
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  打开视频播放页面后即可自动完成所有课程
// @author       luoyulong
// @match        *://*.cmechina.net/*
// @grant        none
// ==/UserScript==

(function () {
  'use strict';
  let KeyQuestions = "questions";
  let urlInfos = window.location.href.split("/");
  let urlTip = urlInfos[urlInfos.length - 1].split("?")[0];
  let courseId = urlInfos[urlInfos.length - 1].split("?")[1];
  let videoUrl = "http://www.cmechina.net/cme/polyv.jsp?"
  let doctor = badDoctor();
  if (urlTip === "polyv.jsp") { //视频页面
    console.log("当前任务: 坏医生看视频");
    doctor.seeVideo()
  } else if (urlTip == "exam.jsp") { //考试页面
    console.log("当前任务: 坏医生考试")
    doctor.doTest()
  } else if (urlTip == "examQuizFail.jsp") { //考试结果页面
    console.log("当前任务: 坏医生考试结果审核")
    setTimeout(function () {
      doctor.doResult();
    }, 1000)
  } else if (urlTip == "examQuizPass.jsp") {
    setTimeout(function () {
      doctor.nexClass()
    }, 1000)

  } else if (urlTip == "course.jsp") {
    setTimeout(function () {
      doctor.selectClass()
    }, 1000)
  } else if (urlTip == "apply.jsp") {
    setTimeout(function () {
      doctor.commentClass()
    }, 1500)

  }else if (urlTip == "ewmface.jsp") 
  {
    setTimeout(function () {
    doctor.faceId()
    },2000)

  }

  function badDoctor() {
    return {
      seeVideo: function () {
        localStorage.removeItem(KeyQuestions)
        setInterval(function () {
          let video = document.querySelector('video')
          if (video.playbackRate < 10) {
            console.log("16倍速")
            video.playbackRate = 16;
          }
          if (video.paused) {
            console.log("检测到暂停,点击播放")
            video.play()
          }
        }, 1000)


        window.s2j_onPlayOver = function () {
          console.log("播放完毕")
          gotoExam();
          console.log("尝试进入考试")
          // showExam(true);
          delCookie("playState");
          addCourseWarePlayRecord();
          setTimeout(function () {
            document.querySelector(".cur").click()
          }, 1000)
        }
      },

      doTest: function () {
        let questionGroup = JSON.parse(localStorage.getItem(KeyQuestions))
        let questionsElem = document.querySelectorAll('li')
        if (questionGroup == null) {
          questionGroup = new Array(5);
          for (let i = 0; i < questionsElem.length; i++) {
            let title = questionsElem[i].innerText.split(/[\s\n]/)[0].replace(RegExp(/\d\./), "")

            questionGroup[i] = {
              "title": title,
              "option": ["A", "B", "C", "D", "E"]
            }
          }
        }
        console.log(questionGroup)
        for (let i = 0; i < questionsElem.length; i++) {
          questionsElem[i].querySelector("input[value='" + questionGroup[i].option[0] + "']").click();
        }
        localStorage.setItem(KeyQuestions, JSON.stringify(questionGroup));  // 存储

        setTimeout(function () {
          document.querySelector("#tjkj").click();
        }, 2000)
      },

      doResult: function () {

        let res = document.querySelector(".show_exam_btns").innerText
        if (res == '重新答题') {
          console.log("考试未通过")

          let questionGroup = JSON.parse(localStorage.getItem(KeyQuestions))

          let wrong_titles = document.querySelector(".show_exam_text").innerText.split(/[\s\n]/)
          for (let i = 0; i < wrong_titles.length; i++) {
            console.log("错误题目:" + wrong_titles[i])

            for (let j = 0; j < questionGroup.length; j++) {

              if (questionGroup[j].title === wrong_titles[i]) {
                console.log("尝试显示：" + questionGroup[j].title)
                let item = {
                  "title": questionGroup[j].title,
                  "option": questionGroup[j].option.splice(1, 10)
                }
                questionGroup[j] = item;
                console.log("移除错误选项" + questionGroup[j].title)
              }
            }
          }
          localStorage.setItem(KeyQuestions, JSON.stringify(questionGroup))
          console.log("save success")
          document.querySelector("#cxdt").click();
        } else {
          console.log("考试通过")
          document.querySelector(".show_exam_btns a").click()
        }
      },
      nexClass: function () {
        setTimeout(function () {
          document.querySelector(".show_exam_btns a").click()
        }, 1000)
      },
      selectClass: function () {
        let unStudy = document.querySelector(".wxx")
        let studying = document.querySelector(".xxz")
        if (unStudy == null && studying == null) {
          alert("Ай-ай-ай-ай-ай, что сейчас произошло!\r\n当前课程已全部学习完成！\r\n" +
            "如未达满分，请手动选择其他课程，学习助手会自动检测并完成学习~~~\r\n" +
            "觉得满意请为项目加个星 \r\nhttps://github.com/luoyulong-CQMU/badDoctor\r\n"
            )
        }
        if (studying!=null)
        {
          let urlClass = studying.parentElement.href.split("/")
          let ids = urlClass[urlClass.length - 1].split("?")[1];
          window.location.href = videoUrl + ids


          // studying.click();
        }else if(unStudy!=null)
        {
          let urlClass = unStudy.parentElement.href.split("/")
          let ids = urlClass[urlClass.length - 1].split("?")[1];
          window.location.href = videoUrl + ids
          // unStudy.click();
        }
      },
      commentClass: function () {
        unsafeWindow.alert = function(){return false};
        window.alert = function(){return false};
        Window.prototype.alert = function(){return false};
        document.querySelector(".bg-lv").click();
      },
      faceId: function () {
      window.location.href = videoUrl + courseId
      }
    }
  }

  // Your code here...
})();
